package com.heatup.apt.api.controller.pay.util.message;

import java.util.HashMap;
import java.util.Map;

import com.heatup.apt.api.controller.pay.util.PropertiesUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.heatup.apt.common.util.TemplateMsgUtil;

import net.sf.json.JSONObject;

public class CustomserviceUtil {
	private static final Logger logger = LoggerFactory.getLogger(CustomserviceUtil.class);
	public static Map senTextMsg(String access_token,String msg,String opendId){
		Map<String,Object> map = new HashMap<String,Object>();
		String urls = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + access_token;
		String canshu = "{\"touser\": \""+opendId+"\", \"msgtype\": \"text\", \"text\": {\"content\": \""+msg+"\"}}";
		try {
			JSONObject json=JSONObject.fromObject(canshu);
			JSONObject jsonResult = TemplateMsgUtil.httpsRequestToJsonObject(urls, "POST", canshu);
			String ticket = null;
			String qr_url = null;
			map.put("errcode", jsonResult.get("errcode"));
			map.put("errmsg", jsonResult.get("errmsg"));
			System.out.println(jsonResult);
			logger.info("=========================jsonResult:"+jsonResult);
		} catch (Exception e) {
			map.put("errcode", "40001");
			map.put("errmsg", "error");
			System.err.println("error:"+e.getMessage());
			logger.info("=========================jsonResult error:"+e.getMessage());
			// TODO: handle exception
		}
		return map;
	}
	
	public static void main(String[] args) {
		String access_token = "CmZTMA0rzVGMt6hsvHXMBvEf5OmqgCRWCnIDs007tFbouAjRw2E_Uy41OjgCinn2RJFIlFiJjyhaJhbuytD7xxVz0moyS6DPxV3rc23k8_kpGR235fedugaSHdyIZ5zcADPjAHAORD";
		String msg = "";
		StringBuffer contentMsg = new StringBuffer();
		contentMsg.append("欢迎加入檬运动！").append("\\n");
		contentMsg.append("您已成功领取了救赎分享的3张共15元的新人代金券。");
		contentMsg.append("<a href='"+String.valueOf(PropertiesUtil.get("coupon_urls")).trim()+"'>查看我的代金券</a>");
//		contentMsg.append("\n");
		contentMsg.append("当您完成第一次锻炼后,救赎也可获得1张共10元的代金券");
		contentMsg.append("<a href='"+String.valueOf(PropertiesUtil.get("course_list")).trim()+"'>我要预约课程</a>");
		Map<String,Object> map = senTextMsg(access_token,contentMsg.toString(),"o0r3rwXCKLvBx3R58IKCkG-HKIM8");
	}
	
}
